﻿namespace TestTool.GUI.Controls
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using TestTool.GUI.Controllers;
    using TestTool.GUI.Data;
    using TestTool.GUI.Enums;
    using TestTool.GUI.Properties;
    using TestTool.GUI.Views;
    using TestTool.Tests.Common.TestEngine;
    using TestTool.Tests.Definitions.Data;
    using TestTool.Tests.Definitions.Interfaces;
    using TestTool.Tests.Engine;
    using System.Linq;

    internal class TestPage : Page, ITestView, IView
    {
        private TestController _controller;
        private VideoContainer _videoWindow;
        private IContainer components;
        private FeaturesTree featuresTree;
        private ToolStripMenuItem runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem;
        private ToolStripMenuItem runCurrentAssumeAllFeaturesSupportedToolStripMenuItem;
        private ToolStripMenuItem runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem;
        private ToolStripMenuItem runSelectedAssumeAllFeaturesSupportedToolStripMenuItem;
        private ToolStripMenuItem saveFeatureDefinitionLogToolStripMenuItem;
        private SplitContainer scMain;
        private TestResultsControl tcTestResults;
        private TabControl tcTestsAndFeatures;
        private ToolStripDropDownButton toolStripDropDownButtonClear;
        private ToolStripDropDownButton toolStripDropDownButtonSave;
        private ToolStripMenuItem toolStripMenuItemClearAll;
        private ToolStripMenuItem toolStripMenuItemClearTestResults;
        private ToolStripMenuItem toolStripMenuItemSaveAll;
        private ToolStripMenuItem toolStripMenuItemSaveCurrent;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator2;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStrip toolStripTestManagement;
        private TabPage tpFeatures;
        private TabPage tpProfiles;
        private TabPage tpTestCases;
        private ToolStripButton tsbHalt;
        private ToolStripButton tsbPause;
        private ToolStripButton tsbQueryFeatures;
        private ToolStripButton tsbRepeatTests;
        private ToolStripButton tsbRunAll;
        private ToolStripSplitButton tsbRunCurrent;
        private ToolStripSplitButton tsbRunSelected;
        private ToolStripButton tsbStop;
        private ProfilesTree tvProfiles;
        private TestsTree tvTestCases;

        public TestPage()
        {
            this.InitializeComponent();
            this._controller = new TestController(this);
        }

        private void AddChildNodes(TreeNode node, List<TestInfo> testInfos)
        {
            if (node.Tag != null)
            {
                TestInfo tag = (TestInfo)node.Tag;
                testInfos.Add(tag);
            }
            foreach (TreeNode node2 in node.Nodes)
            {
                this.AddChildNodes(node2, testInfos);
            }
        }

        public void BeginTest(TestInfo testInfo)
        {
            base.BeginInvoke(new Action(() => this.HighlightActiveTest(testInfo)));
        }

        private void ClearTestInfo()
        {
            this.tcTestResults.ClearTestInfo();
        }

        public void ClearTestResults()
        {
            base.BeginInvoke((Action)delegate
            {
                this.ClearTestInfo();
                this.tvTestCases.ClearTestResults();
                this.tvProfiles.ClearAll();
            });
        }

        private void ClearTestResults(bool bAll)
        {
            this._controller.Clear(bAll);
        }

        private void DisplayTestResults(TestResult log)
        {
            this.tcTestResults.DisplayTestResults(log);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void EnablePage(bool enable)
        {
            base.EnableControls(new Control[] { this.featuresTree, this.tvTestCases });
            foreach (ToolStripButton button in new ToolStripButton[] { this.tsbHalt, this.tsbPause, this.tsbQueryFeatures, this.tsbRunAll, this.tsbRepeatTests, this.tsbStop })
            {
                button.Enabled = enable;
            }
            this.toolStripDropDownButtonClear.Enabled = enable;
            this.toolStripDropDownButtonSave.Enabled = enable;
            this.tsbRunCurrent.Enabled = enable;
            this.tsbRunSelected.Enabled = enable;
            this.tcTestResults.EnableControl(enable);
        }

        private void EnableRunCurrent()
        {
            this.tsbRunCurrent.Enabled = this.tvTestCases.SelectedNode != null;
        }

        private void EnableScrolling(bool enable)
        {
            if (this._controller.Running && enable)
            {
                TestResult currentLog = this._controller.GetCurrentLog();
                if (currentLog != null)
                {
                    this.tcTestResults.ClearTestInfo();
                    this.DisplayTestResults(currentLog);
                }
            }
            this._controller.EnableScrolling(enable);
        }

        public void EnableTestRun(bool enable)
        {
            this.toolStripTestManagement.Enabled = enable;
        }

        public void EndTest(TestResult testResult)
        {
            base.BeginInvoke(new Action(() => this.HighlightCompletedTest(testResult)));
        }

        private void featuresTree_Click(object sender, EventArgs e)
        {
            TestResult log = null;
            if (this.Controller.Running)
            {
                bool flag = (this._controller.CurrentTest != null) && (this._controller.CurrentTest.ProcessType == ProcessType.FeatureDefinition);
                if (this._controller.ScrollingEnabled)
                {
                    if (!flag)
                    {
                        this._controller.EnableScrolling(false);
                    }
                }
                else if (flag)
                {
                    this.EnableScrolling(true);
                }
                else
                {
                    log = this._controller.GetFeaturesDefinitionLog();
                }
            }
            else
            {
                log = this._controller.GetFeaturesDefinitionLog();
            }
            if (log != null)
            {
                this.ClearTestInfo();
                this.DisplayTestResults(log);
            }
            this.tvTestCases.SetInactive();
            this.tvProfiles.SetInactive();
        }

        public override IController GetController()
        {
            return this._controller;
        }

        public IVideoForm GetVideoForm()
        {
            if (this._videoWindow == null)
            {
                VideoContainer container = new VideoContainer();
                this._videoWindow = container;
            }
            return this._videoWindow;
        }

        private void HighlightActiveTest(TestInfo testInfo)
        {
            this.tvTestCases.HighlightActiveTest(testInfo, this._controller.ScrollingEnabled);
        }

        private void HighlightCompletedTest(TestResult testResult)
        {
            this.tvTestCases.HighlightCompletedTest(testResult);
        }

        private void InitializeComponent()
        {
            this.toolStripTestManagement = new ToolStrip();
            this.tsbRunAll = new ToolStripButton();
            this.tsbRunCurrent = new ToolStripSplitButton();
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem = new ToolStripMenuItem();
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem = new ToolStripMenuItem();
            this.tsbRunSelected = new ToolStripSplitButton();
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem = new ToolStripMenuItem();
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem = new ToolStripMenuItem();
            this.tsbQueryFeatures = new ToolStripButton();
            this.tsbRepeatTests = new ToolStripButton();
            this.toolStripSeparator1 = new ToolStripSeparator();
            this.tsbPause = new ToolStripButton();
            this.tsbStop = new ToolStripButton();
            this.tsbHalt = new ToolStripButton();
            this.toolStripSeparator2 = new ToolStripSeparator();
            this.toolStripDropDownButtonClear = new ToolStripDropDownButton();
            this.toolStripMenuItemClearTestResults = new ToolStripMenuItem();
            this.toolStripMenuItemClearAll = new ToolStripMenuItem();
            this.toolStripDropDownButtonSave = new ToolStripDropDownButton();
            this.toolStripMenuItemSaveAll = new ToolStripMenuItem();
            this.toolStripMenuItemSaveCurrent = new ToolStripMenuItem();
            this.saveFeatureDefinitionLogToolStripMenuItem = new ToolStripMenuItem();
            this.toolStripSeparator3 = new ToolStripSeparator();
            this.scMain = new SplitContainer();
            this.tcTestsAndFeatures = new TabControl();
            this.tpTestCases = new TabPage();
            this.tvTestCases = new TestsTree();
            this.tpFeatures = new TabPage();
            this.featuresTree = new FeaturesTree();
            this.tpProfiles = new TabPage();
            this.tvProfiles = new ProfilesTree();
            this.tcTestResults = new TestResultsControl();
            this.toolStripTestManagement.SuspendLayout();
            this.scMain.Panel1.SuspendLayout();
            this.scMain.Panel2.SuspendLayout();
            this.scMain.SuspendLayout();
            this.tcTestsAndFeatures.SuspendLayout();
            this.tpTestCases.SuspendLayout();
            this.tpFeatures.SuspendLayout();
            this.tpProfiles.SuspendLayout();
            base.SuspendLayout();
            this.toolStripTestManagement.GripStyle = ToolStripGripStyle.Hidden;
            this.toolStripTestManagement.Items.AddRange(new ToolStripItem[] { this.tsbRunAll, this.tsbRunCurrent, this.tsbRunSelected, this.tsbQueryFeatures, this.tsbRepeatTests, this.toolStripSeparator1, this.tsbPause, this.tsbStop, this.tsbHalt, this.toolStripSeparator2, this.toolStripDropDownButtonClear, this.toolStripDropDownButtonSave, this.toolStripSeparator3 });
            this.toolStripTestManagement.Location = new Point(0, 0);
            this.toolStripTestManagement.Name = "toolStripTestManagement";
            this.toolStripTestManagement.RenderMode = ToolStripRenderMode.System;
            this.toolStripTestManagement.Size = new Size(0x311, 0x19);
            this.toolStripTestManagement.TabIndex = 1;
            this.toolStripTestManagement.Text = "toolStripTestManagement";
            this.tsbRunAll.Image = Resources.RunAll;
            this.tsbRunAll.ImageTransparentColor = Color.Magenta;
            this.tsbRunAll.Name = "tsbRunAll";
            this.tsbRunAll.Size = new Size(0x30, 0x16);
            this.tsbRunAll.Text = "Run";
            this.tsbRunAll.ToolTipText = "Run all tests";
            this.tsbRunAll.Click += new EventHandler(this.tsbRunAll_Click);
            this.tsbRunCurrent.DropDownItems.AddRange(new ToolStripItem[] { this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem, this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem });
            this.tsbRunCurrent.Enabled = false;
            this.tsbRunCurrent.Image = Resources.RunCurrent;
            this.tsbRunCurrent.ImageTransparentColor = Color.Magenta;
            this.tsbRunCurrent.Name = "tsbRunCurrent";
            this.tsbRunCurrent.Size = new Size(0x67, 0x16);
            this.tsbRunCurrent.Text = "Run Current";
            this.tsbRunCurrent.ToolTipText = "Run current test or group of tests";
            this.tsbRunCurrent.ButtonClick += new EventHandler(this.tsbRunCurrent_Click);
            this.tsbRunCurrent.DropDownOpening += new EventHandler(this.tsbRunCurrent_DropDownOpening);
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem.Name = "runCurrentAssumeAllFeaturesSupportedToolStripMenuItem";
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem.Size = new Size(0x103, 0x16);
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem.Text = "Assume all Features Supported";
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem.Click += new EventHandler(this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem_Click);
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem.Name = "runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem";
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem.Size = new Size(0x103, 0x16);
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem.Text = "Assume all Features Not Supported";
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem.Click += new EventHandler(this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem_Click);
            this.tsbRunSelected.DropDownItems.AddRange(new ToolStripItem[] { this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem, this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem });
            this.tsbRunSelected.Enabled = false;
            this.tsbRunSelected.Image = Resources.RunSelected;
            this.tsbRunSelected.ImageTransparentColor = Color.Magenta;
            this.tsbRunSelected.Name = "tsbRunSelected";
            this.tsbRunSelected.Size = new Size(0x6b, 0x16);
            this.tsbRunSelected.Text = "Run Selected";
            this.tsbRunSelected.ToolTipText = "Run all selected tests";
            this.tsbRunSelected.ButtonClick += new EventHandler(this.tsbRunSelected_Click);
            this.tsbRunSelected.DropDownOpening += new EventHandler(this.tsbRunSelected_DropDownOpening);
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem.Name = "runSelectedAssumeAllFeaturesSupportedToolStripMenuItem";
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem.Size = new Size(0x103, 0x16);
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem.Text = "Assume all Features Supported";
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem.Click += new EventHandler(this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem_Click);
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem.Name = "runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem";
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem.Size = new Size(0x103, 0x16);
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem.Text = "Assume all Features Not Supported";
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem.Click += new EventHandler(this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem_Click);
            this.tsbQueryFeatures.Image = Resources.Refresh;
            this.tsbQueryFeatures.ImageTransparentColor = Color.Magenta;
            this.tsbQueryFeatures.Name = "tsbQueryFeatures";
            this.tsbQueryFeatures.Size = new Size(0x71, 0x16);
            this.tsbQueryFeatures.Text = "Refresh Features";
            this.tsbQueryFeatures.ToolTipText = "Query features supported by the DUT";
            this.tsbQueryFeatures.Click += new EventHandler(this.tsbQueryFeatures_Click);
            this.tsbRepeatTests.AutoSize = false;
            this.tsbRepeatTests.CheckOnClick = true;
            this.tsbRepeatTests.Image = Resources.RunOnce;
            this.tsbRepeatTests.ImageAlign = ContentAlignment.MiddleLeft;
            this.tsbRepeatTests.ImageTransparentColor = Color.Magenta;
            this.tsbRepeatTests.Name = "tsbRepeatTests";
            this.tsbRepeatTests.Size = new Size(0x4e, 0x16);
            this.tsbRepeatTests.Text = "No Repeat";
            this.tsbRepeatTests.Click += new EventHandler(this.tsbRepeatTests_Click);
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new Size(6, 0x19);
            this.tsbPause.Enabled = false;
            this.tsbPause.Image = Resources.Pause;
            this.tsbPause.ImageTransparentColor = Color.Magenta;
            this.tsbPause.Name = "tsbPause";
            this.tsbPause.Size = new Size(0x3a, 0x16);
            this.tsbPause.Text = "Pause";
            this.tsbPause.ToolTipText = "Pause tests execution at IO operation";
            this.tsbPause.Click += new EventHandler(this.tsbPause_Click);
            this.tsbStop.Enabled = false;
            this.tsbStop.Image = Resources.Stop;
            this.tsbStop.ImageTransparentColor = Color.Magenta;
            this.tsbStop.Name = "tsbStop";
            this.tsbStop.Size = new Size(0x33, 0x16);
            this.tsbStop.Text = "Stop";
            this.tsbStop.ToolTipText = "Stop tests execution at the end of test";
            this.tsbStop.Click += new EventHandler(this.tsbStop_Click);
            this.tsbHalt.Enabled = false;
            this.tsbHalt.Image = Resources.Halt;
            this.tsbHalt.ImageTransparentColor = Color.Magenta;
            this.tsbHalt.Name = "tsbHalt";
            this.tsbHalt.Size = new Size(0x31, 0x16);
            this.tsbHalt.Text = "Halt";
            this.tsbHalt.ToolTipText = "Stop tests execution immediately";
            this.tsbHalt.Click += new EventHandler(this.tsbHalt_Click);
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new Size(6, 0x19);
            this.toolStripDropDownButtonClear.DropDownItems.AddRange(new ToolStripItem[] { this.toolStripMenuItemClearTestResults, this.toolStripMenuItemClearAll });
            this.toolStripDropDownButtonClear.Image = Resources.Clear;
            this.toolStripDropDownButtonClear.ImageTransparentColor = Color.Magenta;
            this.toolStripDropDownButtonClear.Name = "toolStripDropDownButtonClear";
            this.toolStripDropDownButtonClear.Size = new Size(0x3f, 0x16);
            this.toolStripDropDownButtonClear.Text = "Clear";
            this.toolStripMenuItemClearTestResults.Image = Resources.ClearTestResults;
            this.toolStripMenuItemClearTestResults.Name = "toolStripMenuItemClearTestResults";
            this.toolStripMenuItemClearTestResults.Size = new Size(0xa6, 0x16);
            this.toolStripMenuItemClearTestResults.Text = "Clear Test Results";
            this.toolStripMenuItemClearTestResults.Click += new EventHandler(this.tsbClear_Click);
            this.toolStripMenuItemClearAll.Image = Resources.Clear;
            this.toolStripMenuItemClearAll.Name = "toolStripMenuItemClearAll";
            this.toolStripMenuItemClearAll.Size = new Size(0xa6, 0x16);
            this.toolStripMenuItemClearAll.Text = "Clear All";
            this.toolStripMenuItemClearAll.Click += new EventHandler(this.tsbClearAll_Click);
            this.toolStripDropDownButtonSave.DropDownItems.AddRange(new ToolStripItem[] { this.toolStripMenuItemSaveAll, this.toolStripMenuItemSaveCurrent, this.saveFeatureDefinitionLogToolStripMenuItem });
            this.toolStripDropDownButtonSave.Image = Resources.Save;
            this.toolStripDropDownButtonSave.ImageTransparentColor = Color.Magenta;
            this.toolStripDropDownButtonSave.Name = "toolStripDropDownButtonSave";
            this.toolStripDropDownButtonSave.Size = new Size(60, 0x16);
            this.toolStripDropDownButtonSave.Text = "Save";
            this.toolStripDropDownButtonSave.ToolTipText = "Save current result";
            this.toolStripDropDownButtonSave.DropDownOpening += new EventHandler(this.toolStripSplitButtonSave_DropDownOpening);
            this.toolStripMenuItemSaveAll.Enabled = false;
            this.toolStripMenuItemSaveAll.Image = Resources.SaveAll;
            this.toolStripMenuItemSaveAll.Name = "toolStripMenuItemSaveAll";
            this.toolStripMenuItemSaveAll.Size = new Size(0xda, 0x16);
            this.toolStripMenuItemSaveAll.Text = "Save All";
            this.toolStripMenuItemSaveAll.ToolTipText = "Save all test results";
            this.toolStripMenuItemSaveAll.Click += new EventHandler(this.toolStripMenuItemSaveAll_Click);
            this.toolStripMenuItemSaveCurrent.Enabled = false;
            this.toolStripMenuItemSaveCurrent.Image = Resources.Save;
            this.toolStripMenuItemSaveCurrent.Name = "toolStripMenuItemSaveCurrent";
            this.toolStripMenuItemSaveCurrent.Size = new Size(0xda, 0x16);
            this.toolStripMenuItemSaveCurrent.Text = "Save Current";
            this.toolStripMenuItemSaveCurrent.ToolTipText = "Save results for currently selected test or group";
            this.toolStripMenuItemSaveCurrent.Click += new EventHandler(this.toolStripMenuItemSave_Click);
            this.saveFeatureDefinitionLogToolStripMenuItem.Enabled = false;
            this.saveFeatureDefinitionLogToolStripMenuItem.Image = Resources.SaveFeatureDefinitionLog;
            this.saveFeatureDefinitionLogToolStripMenuItem.Name = "saveFeatureDefinitionLogToolStripMenuItem";
            this.saveFeatureDefinitionLogToolStripMenuItem.Size = new Size(0xda, 0x16);
            this.saveFeatureDefinitionLogToolStripMenuItem.Text = "Save Feature Definition Log";
            this.saveFeatureDefinitionLogToolStripMenuItem.Click += new EventHandler(this.toolStripMenuItemSaveFeatureDefinitionLog_Click);
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new Size(6, 0x19);
            this.scMain.Dock = DockStyle.Fill;
            this.scMain.Location = new Point(0, 0x19);
            this.scMain.Name = "scMain";
            this.scMain.Panel1.Controls.Add(this.tcTestsAndFeatures);
            this.scMain.Panel2.Controls.Add(this.tcTestResults);
            this.scMain.Size = new Size(0x311, 0x1d0);
            this.scMain.SplitterDistance = 0x132;
            this.scMain.TabIndex = 6;
            this.tcTestsAndFeatures.Alignment = TabAlignment.Left;
            this.tcTestsAndFeatures.Controls.Add(this.tpTestCases);
            this.tcTestsAndFeatures.Controls.Add(this.tpFeatures);
            this.tcTestsAndFeatures.Controls.Add(this.tpProfiles);
            this.tcTestsAndFeatures.Dock = DockStyle.Fill;
            this.tcTestsAndFeatures.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.tcTestsAndFeatures.Location = new Point(0, 0);
            this.tcTestsAndFeatures.Multiline = true;
            this.tcTestsAndFeatures.Name = "tcTestsAndFeatures";
            this.tcTestsAndFeatures.SelectedIndex = 0;
            this.tcTestsAndFeatures.Size = new Size(0x132, 0x1d0);
            this.tcTestsAndFeatures.SizeMode = TabSizeMode.Fixed;
            this.tcTestsAndFeatures.TabIndex = 3;
            this.tcTestsAndFeatures.DrawItem += new DrawItemEventHandler(this.tcTestsAndFeatures_DrawItem);
            this.tpTestCases.Controls.Add(this.tvTestCases);
            this.tpTestCases.Location = new Point(0x17, 4);
            this.tpTestCases.Name = "tpTestCases";
            this.tpTestCases.Padding = new Padding(3);
            this.tpTestCases.Size = new Size(0x117, 0x1c8);
            this.tpTestCases.TabIndex = 0;
            this.tpTestCases.Text = "Test Cases";
            this.tpTestCases.UseVisualStyleBackColor = true;
            this.tvTestCases.CertificationMode = false;
            this.tvTestCases.Dock = DockStyle.Fill;
            this.tvTestCases.Location = new Point(3, 3);
            this.tvTestCases.Name = "tvTestCases";
            this.tvTestCases.SelectedNode = null;
            this.tvTestCases.Size = new Size(0x111, 450);
            this.tvTestCases.TabIndex = 2;
            this.tvTestCases.BeforeCheck += new TreeViewCancelEventHandler(this.tvTestCases_BeforeCheck);
            this.tvTestCases.AfterSelect += new TreeViewEventHandler(this.tvTestCases_AfterSelect);
            this.tvTestCases.AfterCheck += new TreeViewEventHandler(this.tvTestCases_AfterCheck);
            this.tvTestCases.BeforeSelect += new TreeViewCancelEventHandler(this.tvTestCases_BeforeSelect);
            this.tpFeatures.Controls.Add(this.featuresTree);
            this.tpFeatures.Location = new Point(0x17, 4);
            this.tpFeatures.Name = "tpFeatures";
            this.tpFeatures.Padding = new Padding(3);
            this.tpFeatures.Size = new Size(0x117, 0x1c8);
            this.tpFeatures.TabIndex = 1;
            this.tpFeatures.Text = "Features";
            this.tpFeatures.UseVisualStyleBackColor = true;
            this.featuresTree.Dock = DockStyle.Fill;
            this.featuresTree.Location = new Point(3, 3);
            this.featuresTree.Name = "featuresTree";
            this.featuresTree.Size = new Size(0x111, 450);
            this.featuresTree.TabIndex = 0;
            this.featuresTree.BeforeSelect += new TreeViewCancelEventHandler(this.tvTestCases_BeforeSelect);
            this.featuresTree.TreeActivated += new EventHandler(this.featuresTree_Click);
            this.tpProfiles.Controls.Add(this.tvProfiles);
            this.tpProfiles.Location = new Point(0x17, 4);
            this.tpProfiles.Name = "tpProfiles";
            this.tpProfiles.Padding = new Padding(3);
            this.tpProfiles.Size = new Size(0x117, 0x1c8);
            this.tpProfiles.TabIndex = 2;
            this.tpProfiles.Text = "Profiles";
            this.tpProfiles.UseVisualStyleBackColor = true;
            this.tvProfiles.Dock = DockStyle.Fill;
            this.tvProfiles.Location = new Point(3, 3);
            this.tvProfiles.Name = "tvProfiles";
            this.tvProfiles.Size = new Size(0x111, 450);
            this.tvProfiles.TabIndex = 0;
            this.tvProfiles.BeforeSelect += new TreeViewCancelEventHandler(this.tvTestCases_BeforeSelect);
            this.tcTestResults.Dock = DockStyle.Fill;
            this.tcTestResults.Location = new Point(0, 0);
            this.tcTestResults.Name = "tcTestResults";
            this.tcTestResults.Size = new Size(0x1db, 0x1d0);
            this.tcTestResults.TabIndex = 3;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.scMain);
            base.Controls.Add(this.toolStripTestManagement);
            base.Name = "TestPage";
            base.Size = new Size(0x311, 0x1e9);
            this.toolStripTestManagement.ResumeLayout(false);
            this.toolStripTestManagement.PerformLayout();
            this.scMain.Panel1.ResumeLayout(false);
            this.scMain.Panel2.ResumeLayout(false);
            this.scMain.ResumeLayout(false);
            this.tcTestsAndFeatures.ResumeLayout(false);
            this.tpTestCases.ResumeLayout(false);
            this.tpFeatures.ResumeLayout(false);
            this.tpProfiles.ResumeLayout(false);
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public void ReportTestSuiteCompleted()
        {
            base.BeginInvoke(new Action(() => MessageBox.Show(base.FindForm(), "Tests completed", "Done", MessageBoxButtons.OK)));
        }

        private void RunCurrent(FeatureDefinitionMode mode)
        {
            TreeNode selectedNode = this.tvTestCases.SelectedNode;
            if (selectedNode != null)
            {
                TestInfo tag = (TestInfo)selectedNode.Tag;
                TestSuiteParameters parameters = this._controller.GetParameters();
                parameters.FeatureDefinition = mode;
                if (tag != null)
                {
                    parameters.TestCases.Add(tag);
                }
                else
                {
                    List<TestInfo> testInfos = new List<TestInfo>();
                    this.AddChildNodes(selectedNode, testInfos);
                    List<TestInfo> list2 = new List<TestInfo>();
                    list2.AddRange(testInfos);
                    parameters.TestCases.AddRange(from t in list2
                                                  orderby t.Order
                                                  select t);
                }
                this.ClearTestInfo();
                this._controller.RunSingle(parameters);
            }
        }

        private void runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.RunCurrent(FeatureDefinitionMode.AssumeNotSupported);
        }

        private void runCurrentAssumeAllFeaturesSupportedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.RunCurrent(FeatureDefinitionMode.AssumeSupported);
        }

        private void RunSelected(FeatureDefinitionMode mode)
        {
            this.ClearTestResults(false);
            TestSuiteParameters parameters = this._controller.GetParameters();
            parameters.FeatureDefinition = mode;
            parameters.TestCases.AddRange(from TI in this.tvTestCases.SelectedTests
                                          orderby TI.ExecutionOrder, TI.Category, TI.Order
                                          select TI);
            this._controller.Run(parameters);
        }

        private void runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.RunSelected(FeatureDefinitionMode.AssumeNotSupported);
        }

        private void runSelectedAssumeAllFeaturesSupportedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.RunSelected(FeatureDefinitionMode.AssumeSupported);
        }

        private void Save(List<TestResult> results)
        {
            this.SaveInternal(fileName => this._controller.Save(fileName, results));
        }

        private void SaveInternal(Action<string> saveAction)
        {
            SaveFileDialog dialog = new SaveFileDialog
            {
                DefaultExt = ".xml",
                Filter = "XML file | *.xml | All Files | *.*",
                AddExtension = true
            };
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                saveAction(dialog.FileName);
            }
        }

        private void SwitchToIdleMode()
        {
            base.Invoke((Action)delegate
            {
                base.Enabled = true;
                this.tcTestResults.EnableControl(true);
                base.EnableControls(new Control[] { this.tvTestCases, this.featuresTree });
                this.tsbRunSelected.Enabled = this.tvTestCases.SelectedTests.Count > 0;
                this.tsbRunAll.Enabled = true;
                this.tsbQueryFeatures.Enabled = true;
                this.tsbRepeatTests.Enabled = true;
                this.EnableRunCurrent();
                this.tsbPause.Enabled = false;
                this.tsbStop.Enabled = false;
                this.tsbHalt.Enabled = false;
                this.toolStripDropDownButtonClear.Enabled = true;
                this.toolStripDropDownButtonSave.Enabled = true;
                this.Cursor = Cursors.Arrow;
                this.toolStripTestManagement.Cursor = Cursors.Default;
                this.UpdatePauseButton();
            });
        }

        private void SwitchToPausedMode()
        {
            this.tsbRunAll.Enabled = false;
            this.tsbRunCurrent.Enabled = false;
            this.tsbRunSelected.Enabled = false;
            this.tsbQueryFeatures.Enabled = false;
            this.tsbPause.Enabled = true;
            this.tsbStop.Enabled = false;
            this.tsbHalt.Enabled = true;
            this.toolStripDropDownButtonClear.Enabled = false;
            this.toolStripDropDownButtonSave.Enabled = false;
            this.Cursor = Cursors.Arrow;
            this.toolStripTestManagement.Cursor = Cursors.Default;
            this.UpdatePauseButton();
        }

        public override void SwitchToState(TestTool.GUI.Enums.ApplicationState state)
        {
            switch (state)
            {
                case TestTool.GUI.Enums.ApplicationState.Idle:
                    base.BeginInvoke(new Action(this.SwitchToIdleMode));
                    return;

                case TestTool.GUI.Enums.ApplicationState.TestRunning:
                    base.BeginInvoke(new Action(this.SwitchToWorkingMode));
                    return;

                case TestTool.GUI.Enums.ApplicationState.TestPaused:
                    base.BeginInvoke(new Action(this.SwitchToPausedMode));
                    return;

                case TestTool.GUI.Enums.ApplicationState.DiscoveryRunning:
                case TestTool.GUI.Enums.ApplicationState.CommandRunning:
                case TestTool.GUI.Enums.ApplicationState.ConformanceTestRunning:
                    this.EnablePage(false);
                    return;
            }
        }

        private void SwitchToWorkingMode()
        {
            base.Invoke((Action)delegate
            {
                this.tsbRunAll.Enabled = false;
                this.tsbRunCurrent.Enabled = false;
                this.tsbRunSelected.Enabled = false;
                this.tsbQueryFeatures.Enabled = false;
                this.tsbRepeatTests.Enabled = false;
                this.tsbPause.Enabled = true;
                this.tsbStop.Enabled = true;
                this.tsbHalt.Enabled = true;
                this.toolStripDropDownButtonClear.Enabled = false;
                this.toolStripDropDownButtonSave.Enabled = false;
                this.Cursor = Cursors.AppStarting;
                this.toolStripTestManagement.Cursor = Cursors.Default;
                this.UpdatePauseButton();
            });
        }

        private void tcTestsAndFeatures_DrawItem(object sender, DrawItemEventArgs e)
        {
            Brush brush;
            Graphics graphics = e.Graphics;
            TabPage page = this.tcTestsAndFeatures.TabPages[e.Index];
            Rectangle tabRect = this.tcTestsAndFeatures.GetTabRect(e.Index);
            if (e.State == DrawItemState.Selected)
            {
                brush = new SolidBrush(this.tcTestsAndFeatures.ForeColor);
                graphics.FillRectangle(new SolidBrush(this.tcTestsAndFeatures.BackColor), e.Bounds);
            }
            else
            {
                brush = new SolidBrush(e.ForeColor);
                graphics.FillRectangle(new SolidBrush(this.tcTestsAndFeatures.BackColor), e.Bounds);
            }
            Font font = this.tcTestsAndFeatures.Font;
            e.Graphics.RotateTransform(180f);
            tabRect.X = -tabRect.X - tabRect.Width;
            tabRect.Y = -tabRect.Y - tabRect.Height;
            StringFormat format = new StringFormat
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center,
                FormatFlags = StringFormatFlags.DirectionVertical
            };
            graphics.DrawString(page.Text, font, brush, tabRect, new StringFormat(format));
            e.Graphics.ResetTransform();
        }

        private void toolStripMenuItemSave_Click(object sender, EventArgs e)
        {
            List<TestResult> results = new List<TestResult>();
            TreeNode selectedNode = this.tvTestCases.SelectedNode;
            if (selectedNode != null)
            {
                TestInfo tag = (TestInfo)selectedNode.Tag;
                if (tag != null)
                {
                    results.Add(this._controller.GetTestResult(tag));
                }
                else
                {
                    List<TestInfo> testInfos = new List<TestInfo>();
                    this.AddChildNodes(selectedNode, testInfos);
                    foreach (TestInfo info2 in testInfos)
                    {
                        TestResult testResult = this._controller.GetTestResult(info2);
                        if (testResult != null)
                        {
                            results.Add(testResult);
                        }
                    }
                }
            }
            this.Save(results);
        }

        private void toolStripMenuItemSaveAll_Click(object sender, EventArgs e)
        {
            List<TestResult> results = new List<TestResult>();
            foreach (TestInfo info in this._controller.TestInfos)
            {
                TestResult testResult = this._controller.GetTestResult(info);
                if (testResult != null)
                {
                    results.Add(testResult);
                }
            }
            this.Save(results);
        }

        private void toolStripMenuItemSaveFeatureDefinitionLog_Click(object sender, EventArgs e)
        {
            TestResult tr = new TestResult
            {
                Log = this._controller.GetFeaturesDefinitionLog().Log,
                PlainTextLog = this._controller.GetFeaturesDefinitionLog().PlainTextLog
            };
            this.SaveInternal(fileName => this._controller.Save(fileName, tr));
        }

        private void toolStripSplitButtonSave_DropDownOpening(object sender, EventArgs e)
        {
            this.toolStripMenuItemSaveAll.Enabled = this._controller.HasTestResults();
            this.saveFeatureDefinitionLogToolStripMenuItem.Enabled = this._controller.GetFeaturesDefinitionLog() != null;
            TreeNode selectedNode = this.tvTestCases.SelectedNode;
            if (selectedNode == null)
            {
                this.toolStripMenuItemSaveCurrent.Enabled = false;
            }
            else
            {
                bool flag = false;
                if (selectedNode.Tag != null)
                {
                    TestInfo tag = (TestInfo)selectedNode.Tag;
                    flag = this._controller.GetTestResult(tag) != null;
                }
                else
                {
                    List<TestInfo> testInfos = new List<TestInfo>();
                    this.AddChildNodes(selectedNode, testInfos);
                    foreach (TestInfo info2 in testInfos)
                    {
                        if (this._controller.GetTestResult(info2) != null)
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                this.toolStripMenuItemSaveCurrent.Enabled = flag;
            }
        }

        private void tsbClear_Click(object sender, EventArgs e)
        {
            this.ClearTestResults(false);
        }

        private void tsbClearAll_Click(object sender, EventArgs e)
        {
            this.ClearTestResults(true);
        }

        private void tsbHalt_Click(object sender, EventArgs e)
        {
            this._controller.Halt();
        }

        private void tsbPause_Click(object sender, EventArgs e)
        {
            this._controller.Pause();
            this.tsbPause.Enabled = false;
        }

        private void tsbQueryFeatures_Click(object sender, EventArgs e)
        {
            DialogResult yes = DialogResult.Yes;
            if (this._controller.HasTestResults())
            {
                yes = MessageBox.Show(base.FindForm(), "Test results wil be cleared. Continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            }
            if (yes == DialogResult.Yes)
            {
                this.ClearTestResults(true);
                this._controller.ClearFeatures();
                this.featuresTree.Clear();
                this._controller.DefineFeatures(this._controller.GetParameters());
            }
        }

        private void tsbRepeatTests_Click(object sender, EventArgs e)
        {
            bool flag = this.tsbRepeatTests.Checked;
            this.tsbRepeatTests.Text = flag ? "Repeat " : "No Repeat";
            this.tsbRepeatTests.Image = flag ? Resources.RepeatTest : Resources.RunOnce;
        }

        private void tsbRunAll_Click(object sender, EventArgs e)
        {
            this._controller.RunAll();
        }

        private void tsbRunCurrent_Click(object sender, EventArgs e)
        {
            this.RunCurrent(FeatureDefinitionMode.Default);
        }

        private void tsbRunCurrent_DropDownOpening(object sender, EventArgs e)
        {
            this.runCurrentAssumeAllFeaturesSupportedToolStripMenuItem.Enabled = !this._controller.FeaturesDefined;
            this.runCurrentAssumeAllFeaturesNotSupportedToolStripMenuItem.Enabled = !this._controller.FeaturesDefined;
        }

        private void tsbRunSelected_Click(object sender, EventArgs e)
        {
            this.RunSelected(FeatureDefinitionMode.Default);
        }

        private void tsbRunSelected_DropDownOpening(object sender, EventArgs e)
        {
            this.runSelectedAssumeAllFeaturesSupportedToolStripMenuItem.Enabled = !this._controller.FeaturesDefined;
            this.runSelectedAssumeAllFeaturesNotSupportedToolStripMenuItem.Enabled = !this._controller.FeaturesDefined;
        }

        private void tsbStop_Click(object sender, EventArgs e)
        {
            this.tsbStop.Enabled = false;
            this._controller.Stop();
        }

        private void tvProfiles_ProfileSelected(IProfileDefinition profile)
        {
            if (!this.Controller.Running)
            {
                if (profile != null)
                {
                    ProfileTestInfo profileInformation = this._controller.GetProfileInformation(profile);
                    if (profileInformation != null)
                    {
                        this.tcTestResults.DisplayProfileTestLog(profileInformation.Log);
                    }
                }
                this.tvTestCases.SetInactive();
                this.featuresTree.SetInactive();
            }
        }

        private void tvTestCases_AfterCheck(object sender, TreeViewEventArgs e)
        {
            this.tsbRunSelected.Enabled = !this._controller.Running && (this.tvTestCases.SelectedTests.Count > 0);
        }

        private void tvTestCases_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = this.tvTestCases.SelectedNode;
            if (selectedNode != null)
            {
                if (selectedNode.Tag != null)
                {
                    TestInfo tag = (TestInfo)selectedNode.Tag;
                    bool flag = true;
                    if (this._controller.Running)
                    {
                        if (this._controller.ScrollingEnabled)
                        {
                            if (this._controller.CurrentTest != tag)
                            {
                                this.EnableScrolling(false);
                            }
                        }
                        else if (this._controller.CurrentTest == tag)
                        {
                            this.EnableScrolling(true);
                            flag = false;
                        }
                        flag = !this._controller.ScrollingEnabled;
                    }
                    if (flag)
                    {
                        this.ClearTestInfo();
                        TestResult testResult = this._controller.GetTestResult(tag);
                        if (testResult != null)
                        {
                            this.DisplayTestResults(testResult);
                        }
                    }
                }
                else if (!this._controller.Running || !this._controller.ScrollingEnabled)
                {
                    this.ClearTestInfo();
                }
                this.tvProfiles.SetInactive();
                this.featuresTree.SetInactive();
                this.tsbRunCurrent.Enabled = !this._controller.Running;
            }
        }

        private void tvTestCases_BeforeCheck(object sender, TreeViewCancelEventArgs e)
        {
            if (this._controller.Running)
            {
                e.Cancel = true;
            }
        }

        private void tvTestCases_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
        }

        private void UpdatePauseButton()
        {
            if (this._controller.State == TestState.Paused)
            {
                this.tsbPause.Text = "Continue";
                this.tsbPause.ToolTipText = "Continue tests execution";
                this.tsbPause.Image = Resources.RunCurrent;
            }
            else
            {
                this.tsbPause.Text = "Pause";
                this.tsbPause.ToolTipText = "Pause tests execution at IO operation";
                this.tsbPause.Image = Resources.Pause;
            }
        }

        internal TestController Controller
        {
            get
            {
                return this._controller;
            }
        }

        public IFeaturesView FeaturesView
        {
            get
            {
                return this.featuresTree;
            }
        }

        public IProfilesView ProfilesView
        {
            get
            {
                return this.tvProfiles;
            }
        }

        public bool Repeat
        {
            get
            {
                return this.tsbRepeatTests.Checked;
            }
            set
            {
                this.tsbRepeatTests.Checked = value;
            }
        }

        public ITestResultView TestResultView
        {
            get
            {
                return this.tcTestResults;
            }
        }

        public ITestTreeView TestTreeView
        {
            get
            {
                return this.tvTestCases;
            }
        }

        public Form Window
        {
            get
            {
                return base.FindForm();
            }
        }
    }
}

